package com.check.checksamejourney.service.impl;

import com.check.checksamejourney.bean.Journey;
import com.check.checksamejourney.mapper.JourneyMapper;
import com.check.checksamejourney.service.IJourneyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author Bob
 * @desc
 * @date 2020/6/4 22:30
 */
@Service("journeyService")
public class JourneyService implements IJourneyService{
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private JourneyMapper journeyMapper;

    private final static String JOURNEY_DATA = "JOURNEY_DATA_";

    @Override
    public Journey getJourneyById(Long id) {
        String key = JOURNEY_DATA + id;
        ValueOperations<String, Journey> operations = redisTemplate.opsForValue();
        if (redisTemplate.hasKey(key)) {
            return operations.get(key);
        }else {
            Journey journey = journeyMapper.getJourneyById(id);
            operations.set(key, journey,6,  TimeUnit.HOURS);
        }
        return operations.get(key);
    }
}
